Templates

The notification template is used to generate the notification content and is configured in the argocd-notifications-cm ConfigMap. The template is leveraging the html/template golang package and allows customization of the notification message. Templates are meant to be reusable and can be referenced by multiple triggers.

The following template is used to notify the user about application sync status.

  1. apiVersion: v1
  2. kind: ConfigMap
  3. metadata:
  4. name: argocd-notifications-cm
  5. data:
  6. template.my-custom-template-slack-template: |
  7. message: |
  8. Application {{.app.metadata.name}} sync is {{.app.status.sync.status}}.
  9. Application details: {{.context.argocdUrl}}/applications/{{.app.metadata.name}}.

Each template has access to the following fields:

  • app holds the application object.
  • context is a user-defined string map and might include any string keys and values.
  • secrets provides access to sensitive data stored in argocd-notifications-secret
  • serviceType holds the notification service type name (such as “slack” or “email). The field can be used to conditionally render service-specific fields.
  • recipient holds the recipient name.

Defining user-defined context

It is possible to define some shared context between all notification templates by setting a top-level YAML document of key-value pairs, which can then be used within templates, like so:

  1. apiVersion: v1
  2. kind: ConfigMap
  3. metadata:
  4. name: argocd-notifications-cm
  5. data:
  6. context: |
  7. region: east
  8. environmentName: staging
  9. template.a-slack-template-with-context: |
  10. message: "Something happened in {{ .context.environmentName }} in the {{ .context.region }} data center!"

Defining and using secrets within notification templates

Some notification service use cases will require the use of secrets within templates. This can be achieved with the use of the secrets data variable available within the templates.

Given that we have the following argocd-notifications-secret:

  1. apiVersion: v1
  2. kind: Secret
  3. metadata:
  4. name: argocd-notifications-secret
  5. stringData:
  6. sampleWebhookToken: secret-token
  7. type: Opaque

We can use the defined sampleWebhookToken in a template as such:

  1. apiVersion: v1
  2. kind: ConfigMap
  3. metadata:
  4. name: argocd-notifications-cm
  5. data:
  6. template.trigger-webhook: |
  7. webhook:
  8. sample-webhook:
  9. method: POST
  10. path: 'webhook/endpoint/with/auth'
  11. body: 'token={{ .secrets.sampleWebhookToken }}&variables[APP_SOURCE_PATH]={{ .app.spec.source.path }}

Notification Service Specific Fields

The message field of the template definition allows creating a basic notification for any notification service. You can leverage notification service-specific fields to create complex notifications. For example using service-specific you can add blocks and attachments for Slack, subject for Email or URL path, and body for Webhook. See corresponding service documentation for more information.

Change the timezone

You can change the timezone to show in notifications as follows.

  1. Call time functions.

    1. {{ (call .time.Parse .app.status.operationState.startedAt).Local.Format "2006-01-02T15:04:05Z07:00" }}
  2. Set the TZ environment variable on the argocd-notifications-controller container.

    1. apiVersion: apps/v1
    2. kind: Deployment
    3. metadata:
    4. name: argocd-notifications-controller
    5. spec:
    6. template:
    7. spec:
    8. containers:
    9. - name: argocd-notifications-controller
    10. env:
    11. - name: TZ
    12. value: Asia/Tokyo

Functions

Templates have access to the set of built-in functions:

  1. apiVersion: v1
  2. kind: ConfigMap
  3. metadata:
  4. name: argocd-notifications-cm
  5. data:
  6. template.my-custom-template-slack-template: |
  7. message: "Author: {{(call .repo.GetCommitMetadata .app.status.sync.revision).Author}}"

time

Time related functions.


time.Now() Time

Executes function built-in Golang time.Now function. Returns an instance of Golang Time.


time.Parse(val string) Time

Parses specified string using RFC3339 layout. Returns an instance of Golang Time.

strings

String related functions.


strings.ReplaceAll() string

Executes function built-in Golang strings.ReplaceAll function.


strings.ToUpper() string

Executes function built-in Golang strings.ToUpper function.


strings.ToLower() string

Executes function built-in Golang strings.ToLower function.

sync


sync.GetInfoItem(app map, name string) string Returns the info item value by given name stored in the Argo CD App sync operation.

repo

Functions that provide additional information about Application source repository.


repo.RepoURLToHTTPS(url string) string

Transforms given GIT URL into HTTPs format.


repo.FullNameByRepoURL(url string) string

Returns repository URL full name (<owner>/<repoName>). Currently supports only Github, GitLab and Bitbucket.


repo.QueryEscape(s string) string

QueryEscape escapes the string, so it can be safely placed inside a URL

Example:

  1. /projects/{{ call .repo.QueryEscape (call .repo.FullNameByRepoURL .app.status.RepoURL) }}/merge_requests

repo.GetCommitMetadata(sha string) CommitMetadata

Returns commit metadata. The commit must belong to the application source repository. CommitMetadata fields:

  • Message string commit message
  • Author string - commit author
  • Date time.Time - commit creation date
  • Tags []string - Associated tags

repo.GetAppDetails() AppDetail

Returns application details. AppDetail fields:

  • Type string - AppDetail type
  • Helm HelmAppSpec - Helm details
  • Fields :
    • Name string
    • ValueFiles []string
    • Parameters []*v1alpha1.HelmParameter
    • Values string
    • FileParameters []*v1alpha1.HelmFileParameter
  • Methods :
    • GetParameterValueByName(Name string) Retrieve value by name in Parameters field
    • GetFileParameterPathByName(Name string) Retrieve path by name in FileParameters field *
  • Kustomize *apiclient.KustomizeAppSpec - Kustomize details
  • Directory *apiclient.DirectoryAppSpec - Directory details